// ethers v6
const { ethers } = require("ethers");

const simulateCheckUpkeep = async (contract, args) => {
  // use provider.call send a 'static call' request, don't change the state of the contract
  // 需要构造交易请求的 calldata

  // 1. 编码函数调用数据
  // 使用 ethers.js 的 interface.encodeFunctionData 方法来编码函数调用数据
  const data = contract.interface.encodeFunctionData("checkUpkeep", args);

  // 2. 构造 call 请求
  const callRequest = {
    to: contract.target,  // 合约地址
    data: data,
  };

  // 3. 静态调用
  const provider = contract.runner.provider;
  const result = await provider.call(callRequest);

  // 4. 解码返回值
  const decoded = contract.interface.decodeFunctionResult("checkUpkeep", result);

  return decoded;
}

module.exports = {
  simulateCheckUpkeep,
}
